package org.project.s.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.CommentRecord;
import org.project.pojo.Fengye;
import org.project.service.user.CommentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Controller
@RequestMapping("/commentcontroller")
public class Commentcontroller {
@Resource
private CommentService commentService;

@RequestMapping("/selectcomment")
public String selectcomment(HttpServletRequest request, HttpServletResponse response){
	HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	
	
	String str=request.getParameter("n");   
//	System.out.println(str);
	Fengye fengye=new Fengye();
     //总行数  
     int lineCount=commentService.selectcommentcount();
     //每页的行数  
     int pageSize=10;  
     //总共的页数  
     int pageCount=(int)Math.ceil(lineCount*1.0/pageSize);  
       
     int n = 0;  
               
     if(str == null || str.equals("")){  
         n = 0;  
     }else if(str.equals("a")){  
    	 
          n = (Integer)request.getSession().getAttribute("pageNow");  
         n=n-pageSize;  
     }else if(str.equals("b")){  
          n = (Integer)request.getSession().getAttribute("pageNow");  
         n=n+pageSize;  
     }else if(str.equals("0")){  
         n=0;  
     }else{  
         n=(pageCount-1)*pageSize;  
     }     
       
     if(n>((pageCount-1)*pageSize)){  
         n=(pageCount-1)*pageSize;  
     }  
     if(n<0){  
         n=0;  
     }     
       
       
     request.getSession().setAttribute("pageNow", n);  //当前页首行
     request.getSession().setAttribute("pageCount", pageCount);    
       
   
   
     fengye.setN(n);
     fengye.setPageSize(pageSize);
     List<CommentRecord> commentlist= commentService.selectcomment(fengye);
    
    
	 
	       req.setAttribute("commentlist", commentlist);
	       req.setAttribute("commentcount", commentService.selectcommentcount());
          
	       return "showcomment";
	
	
}





@RequestMapping("/deletecomment")
public String deletecomment(@Param("id")String id){
	CommentRecord commentRecord=new CommentRecord();
	commentRecord.setId(Integer.parseInt(id));
	int i=commentService.deletecomment(commentRecord);
	
	if(i>0){
		
		return "redirect:/commentcontroller/selectcomment";
	}else{
		return "";
	}
	
}
















}
